{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e7389dad",
   "metadata": {},
   "source": [
    "#### NumPy练习题2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "eb911f0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "78aebffe",
   "metadata": {},
   "source": [
    "1. 给定一个4维矩阵，如何得到最后两维的和？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ea850d17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[89, 84, 69],\n",
       "       [96, 90, 62]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.random.randint(0, 10, size=(2,3, 4, 5))\n",
    "n\n",
    "n.sum(axis=(2,3))\n",
    "# 第一个维度 axis = 0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17becd65",
   "metadata": {},
   "source": [
    "2. 给定数组[1, 2, 3, 4, 5], 如何得到在这个数组的每个元素之间插入3个0后的新数组？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "776d8695",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5], dtype=int16)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.arange(1, 6)\n",
    "n2 = np.zeros(12 + 5, dtype=np.int16)\n",
    "# 通过切片取到值再赋值\n",
    "n2[::4] = n\n",
    "n2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cdd21ef3",
   "metadata": {},
   "source": [
    "3. 给定一个二维矩阵，如何交换其中两行的元素？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f4a51a9d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 0, 3, 1],\n",
       "       [9, 8, 8, 4],\n",
       "       [1, 9, 6, 4],\n",
       "       [7, 9, 0, 5]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[9, 8, 8, 4],\n",
       "       [6, 0, 3, 1],\n",
       "       [1, 9, 6, 4],\n",
       "       [7, 9, 0, 5]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n = np.random.randint(0, 10, size=(4, 4))\n",
    "display(n)\n",
    "\n",
    "# 可以利用索引来交换行\n",
    "n2 = n[[1,0,2,3]]\n",
    "display(n2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "38a2f3b7",
   "metadata": {},
   "source": [
    "4. 创建一个100000长度的随机数组，使用两种方法对其求三次方，并比较所用时间。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "bb276d50",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "173 µs ± 401 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "n = np.random.randint(0, 10, size=100000)\n",
    "# display(n)\n",
    "\n",
    "# 计算方式1\n",
    "%timeit np.power(n, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "de608a15",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "174 µs ± 652 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "# 计算方式2\n",
    "%timeit n**3\n",
    "\n",
    "# 结论：这两种计算方式消耗时间差不多，都可以用。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "45a55468",
   "metadata": {},
   "source": [
    "5. 创建一个5 * 3随机矩阵和一个3 * 2随机矩阵，求矩阵积。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b819658e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 2520,   916],\n",
       "       [ 8356,  2998],\n",
       "       [10125,  3705],\n",
       "       [ 3519,  1321],\n",
       "       [ 6004,  2089]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 = np.random.randint(0, 100, size=(5, 3))\n",
    "n2 = np.random.randint(0, 100, size=(3, 2))\n",
    "np.dot(n1, n2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61c17211",
   "metadata": {},
   "source": [
    "6. 矩阵的每一行的元素都减去该行的平均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "fee48f60",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 5, 4],\n",
       "       [4, 4, 8],\n",
       "       [3, 5, 0]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[3.33333333],\n",
       "       [5.33333333],\n",
       "       [2.66666667]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[-2.33333333,  1.66666667,  0.66666667],\n",
       "       [-1.33333333, -1.33333333,  2.66666667],\n",
       "       [ 0.33333333,  2.33333333, -2.66666667]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.random.randint(0, 10, size=(3, 3))\n",
    "display(n)\n",
    "\n",
    "n2 = n.mean(axis=1) # 求的每一行的多列的平均值。不要纠结，记住就可以。\n",
    "#display(n2)\n",
    "n2 = n2.reshape(3, 1)\n",
    "display(n2)\n",
    "n - n2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "babf1fe0",
   "metadata": {},
   "source": [
    "7. 打印出以下矩阵（要求使用np.zeros创建8 * 8的矩阵）\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "534d8c40",
   "metadata": {},
   "source": [
    "      [[0, 1, 0, 1, 0, 1, 0, 1],\n",
    "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
    "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
    "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
    "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
    "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
    "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
    "       [1, 0, 1, 0, 1, 0, 1, 0]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "40341643",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0],\n",
       "       [0, 1, 0, 1, 0, 1, 0, 1],\n",
       "       [1, 0, 1, 0, 1, 0, 1, 0]], dtype=int16)"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.zeros((8, 8), dtype=np.int16)\n",
    "n[::2, 1::2] = 1\n",
    "#n\n",
    "n[1::2, ::2] = 1\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "786758c5",
   "metadata": {},
   "source": [
    "8. 正则化一个 5 * 5的随机矩阵\n",
    "- 正则的概念：假设a是矩阵中的一个元素，max/min分别是矩阵元素的最大值最小值，则正则化后a = (a - min) / (max - min)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "d1c34d6d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[40, 72, 96, 46, 59],\n",
       "       [74, 45, 59,  5, 97],\n",
       "       [19, 21, 68, 81, 30],\n",
       "       [66, 18, 37, 81, 30],\n",
       "       [46, 61, 28, 17, 65]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n = np.random.randint(0, 100, size=(5, 5))\n",
    "display(n)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "bcd3aa3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5, 97)"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min1 = n.min()\n",
    "max1 = n.max()\n",
    "min1, max1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "045cee24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.38043478, 0.72826087, 0.98913043, 0.44565217, 0.58695652],\n",
       "       [0.75      , 0.43478261, 0.58695652, 0.        , 1.        ],\n",
       "       [0.15217391, 0.17391304, 0.68478261, 0.82608696, 0.27173913],\n",
       "       [0.66304348, 0.14130435, 0.34782609, 0.82608696, 0.27173913],\n",
       "       [0.44565217, 0.60869565, 0.25      , 0.13043478, 0.65217391]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 正则化 a = (a - min) / (max - min)\n",
    "(n - min1) / (max1 - min1)\n",
    "# 这样可以把每个值压缩到[0, 1]。正则化也可以叫归一化。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
